package utils;

import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;

import java.util.List;
/**
 * @author
 */
public class MessageBubble {


    public HBox setOnLeft(String message, String account) {
        Label messageBox = new Label();
        messageBox.setStyle(" -fx-background-image: url(\"image/11.png\");");
        Label messageBox1 = new Label();
        HBox h = new HBox();
        h.setAlignment(Pos.BOTTOM_LEFT);
        ImageView i1 = new ImageView("file:photo/" + account + ".png");
        double height = getHight(message);
        double width = getWidth(message);
        i1.setFitHeight(35);
        i1.setFitWidth(35);
        VBox v = new VBox();
        v.getChildren().add(i1);
        h.setSpacing(10);
        messageBox.setPrefHeight(height);
        messageBox.setPrefWidth(width);
        messageBox.setText(message);
        messageBox.setAlignment(Pos.TOP_LEFT);
        Font font = new Font(15);
        messageBox.setFont(font);
        h.getChildren().addAll(v, messageBox);
        return h;
    }

    public HBox setOnRight(String message, String account ){

        Label messageBox = new Label();
        messageBox.setStyle(" -fx-background-image: url(\"image/11.png\");");
        Label messageBox1 = new Label();
        HBox h = new HBox();
        h.setAlignment(Pos.BOTTOM_RIGHT);
        ImageView i1 = new ImageView("file:photo/" + account + ".png");
        double width = getWidth(message);
        double height = getHight(message);
        i1.setFitHeight(35);
        i1.setFitWidth(35);
        VBox v = new VBox();
        v.getChildren().add(i1);
        h.setSpacing(10);
        messageBox.setPrefHeight(height);
        messageBox.setPrefWidth(width);
        messageBox.setText(message);
        Font font = new Font(15);
        messageBox.setFont(font);
        messageBox.setAlignment(Pos.TOP_LEFT);
        h.getChildren().addAll(messageBox, v);
        return h;
    }

    public HBox setTime(String date) {
        HBox h1 = new HBox();
        h1.setAlignment(Pos.CENTER);
        Label l = new Label(date);
        h1.getChildren().add(l);
        return h1;
    }

    public static double getWidth(String msg) {//获得宽度

        int len = msg.length();
        double width = 20;
        for (int i = 0; i < len; i++) {
            if (msg.charAt(i) == '\n') {
                break;
            }
            if (isChinese(msg.charAt(i))) {
                width += 17;
            } else {
                width += 9;
            }
        }
        int widthBound=480;
        //29中 17=15px/64英 8=7px
        if (width <= widthBound)
        {
            return width;
        } else {
            return 480;
        }
    }

    public static double getHight(String msg) {
        int len = msg.length();
        double width = 20;
        double height = 40;
        for (int i = 0; i < len; i++) {

            if (isChinese(msg.charAt(i))) {
                width += 17;
            } else {
                width += 9;
            }
            if (msg.charAt(i) == '\n') {
                height += 17.4;
            }
            if (width >= 480) {
                height += 17.4;
                width = 20;
            }

        }
        return height;

    }

    private static final boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }
        return false;
    }
}
